/*
Created by =BTC= Giallustio
version 0.6
Visit us at: 
http://www.blacktemplars.altervista.org/
06/03/2012
*/

////////////////// EDITABLE \\\\\\\\\\\\\\\\\\\\\\\\\\
BTC_revive_time_min = 5;
BTC_revive_time_max = 600;
BTC_who_can_revive  = ["Man"];
BTC_disable_respawn = 0;
BTC_respawn_gear    = 1;
BTC_active_lifes    = 1;
BTC_lifes           = 999;
BTC_black_screen    = 0;//Black screen + button while unconscious or action wheel and clear view
BTC_action_respawn  = 1;//if black screen is set to 0 you can choose if you want to use the action wheel or the button. Keep in mind that if you don't use the button, the injured player can use all the action, frag too....
BTC_respawn_time    = 5;
BTC_active_mobile   = 1;//Active mobile respawn (You have to put in map the vehicle and give it a name. Then you have to add one object per side to move to the mobile (BTC_base_flag_west,BTC_base_flag_east) - (1 = yes, 0 = no))
BTC_mobile_respawn  = 1;//Active the mobile respawn fnc (1 = yes, 0 = no)
BTC_mobile_respawn_time = 300;//Secs delay for mobile vehicle to respawn
BTC_need_first_aid = 0;//You need a first aid kit to revive (1 = yes, 0 = no)
BTC_pvp = 0; //(disable the revive option for the enemy)
BTC_injured_marker = 1;
BTC_vehs_mobile_west = [];//Editable - define mobile west
BTC_vehs_mobile_east = [];//Editable - define mobile east
BTC_score_change = 5; // The number of points you loose when respawning or gain when reviving
////////////////// Don't edit below \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
if (side player == west) then {
	if (!isnil "QG_Mobile") then { BTC_vehs_mobile_west = [QG_Mobile]; };
} else {
	if (!isnil "QG_Mobile") then { BTC_vehs_mobile_east = [QG_Mobile]; };
};

//FNC
call compile preprocessFile "base\support\modules\btc_revive\=BTC=_functions.sqf";

if (isServer) then
{
	//Mobile
	if (BTC_active_mobile == 1 && count BTC_vehs_mobile_west != 0) then {
		for "_i" from 0 to ((count BTC_vehs_mobile_west) - 1) do {
			_veh = (BTC_vehs_mobile_west select _i);
			if (!isnil "_veh") then {
				if (!isnull _veh) then {
					_var = str (_veh);
					_veh setVariable ["BTC_mobile_west",_var,true];
					if (BTC_mobile_respawn == 1) then {
						_resp = [_veh,_var,"BTC_mobile_west"] spawn BTC_vehicle_mobile_respawn;
					};
				};
			};
		};
	} else {
		{deleteVehicle _x} foreach BTC_vehs_mobile_west;
	};
	if (BTC_active_mobile == 1 && count BTC_vehs_mobile_east != 0) then {
		for "_i" from 0 to ((count BTC_vehs_mobile_east) - 1) do {
			_veh = (BTC_vehs_mobile_east select _i);
			if (!isnil "_veh") then {
				if (!isnull _veh) then {
					_var = str (_veh);
					_veh setVariable ["BTC_mobile_east",_var,true];
					if (BTC_mobile_respawn == 1) then {
						_resp = [_veh,_var,"BTC_mobile_east"] spawn BTC_vehicle_mobile_respawn;
					};
				};
			};
		};
	} else {
		{deleteVehicle _x} foreach BTC_vehs_mobile_east;
	};
	BTC_killed_pveh = [];publicVariable "BTC_killed_pveh";
	BTC_drag_pveh = [];publicVariable "BTC_drag_pveh";
	BTC_marker_pveh = [];publicVariable "BTC_marker_pveh";
	BTC_score_pveh = [];publicVariable "BTC_score_pveh";
	"BTC_score_pveh" addPublicVariableEventHandler BTC_fnc_PVEH;
};
if (isDedicated) exitWith {};
BTC_dragging = false;
BTC_respawn_cond = false;
//Init
[] spawn
{
	waitUntil {!isNull player};
	waitUntil {player == player};
	"BTC_drag_pveh" addPublicVariableEventHandler BTC_fnc_PVEH;
	"BTC_marker_pveh" addPublicVariableEventHandler BTC_fnc_PVEH;
	"BTC_killed_pveh" addPublicVariableEventHandler BTC_fnc_PVEH;
	player addRating 9999;
	
	// We add a trigger on the player when he gets near the mobile respawn, so he can heal himself.
	BTC_MHQ_HealAction = -1;
	_trg= createTrigger["EmptyDetector",[0,0,0]];
	_trg setTriggerArea[0,0,0,false];
	_trg setTriggerActivation["ANY","PRESENT",true];
	_trg setTriggerStatements["call BTC_fnc_MobileHQNear", "if (BTC_MHQ_HealAction < 0) then { call BTC_fnc_AddHealAction; };", "if (BTC_MHQ_HealAction >= 0) then { call BTC_fnc_RemoveHealAction; };"]; 
	
	BTC_side = side player;
	BTC_respawn_marker = format ["respawn_%1",side player];
	if (BTC_respawn_gear == 1) then {player addEventHandler ["HandleDamage", BTC_fnc_handledamage];};
	// player addEventHandler ["HandleDamage", BTC_fnc_handledamage];
	player addEventHandler ["Killed", BTC_player_killed];
	player setVariable ["BTC_need_revive",0,true];
	if ([player] call BTC_is_class_can_revive) then {player addAction [("<t color=""#ED2744"">") + (localize "STR_REVIVE") + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[],BTC_first_aid], 8, true, true, "", "[] call BTC_check_action_first_aid"];};
	player addAction [("<t color=""#ED2744"">") + (localize "STR_DRAG_BODY") + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[],BTC_drag], 8, true, true, "", "[] call BTC_check_action_drag"];
	if (BTC_active_mobile == 1) then {
		if (side player == west) then {
			{
				_spawn = [str (_x)] spawn BTC_mobile_marker;
				_x addEventHandler ["getin",{if (player == driver (_this select 0)) then{ hint Format[(localize "STR_ENTERED_MHQ"),vehicleVarName (_this select 0)];};}];
				BTC_base_flag_west addAction [("<t color=""#ED2744"">") + (localize "STR_MOVE_TO_MOBILE" + format ["%1",_x]) + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[str (_x)],BTC_move_to_mobile], 10000, true, true, "", format ["[str (%1)] call BTC_mobile_check",_x]];
			} foreach BTC_vehs_mobile_west;
		} else {
			{
				_spawn = [str (_x)] spawn BTC_mobile_marker;
				BTC_base_flag_east addAction [("<t color=""#ED2744"">") + (localize "STR_MOVE_TO_MOBILE" + format ["%1",_x]) + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[str (_x)],BTC_move_to_mobile], 10000, true, true, "", format ["[str (%1)] call BTC_mobile_check",_x]];
				_x addEventHandler ["getin",{if (player == driver (_this select 0)) then{ hint Format[(localize "STR_ENTERED_MHQ"),vehicleVarName (_this select 0)];};}];
			} foreach BTC_vehs_mobile_east;
		};
	};
	BTC_weapons = weapons player;
	BTC_prim_weap = primaryWeapon player;
	BTC_prim_items = primaryWeaponItems player;
	BTC_sec_weap = secondaryWeapon player;
	BTC_sec_items = secondaryWeaponItems player;
	BTC_items_assigned = assignedItems player;
		//BTC_items = items _player;
	BTC_handgun = handgunWeapon player;
	BTC_handgun_items = handgunItems player;
	BTC_mags = magazines player;
	BTC_goggles = goggles player;
	BTC_headgear = headgear player;
	BTC_uniform = uniform player;
	BTC_uniform_items = uniformItems player;
	BTC_vest = vest player;
	BTC_vest_items = vestItems player;
	BTC_back_pack = backpack player;
	BTC_back_pack_items = backpackItems player;
	BTC_back_pack_weap = getWeaponCargo (unitBackpack player);
	BTC_weap_sel = currentWeapon player;
	BTC_at_mag = "";BTC_r_mag = "";BTC_h_mag = "";
	if (BTC_sec_weap != "") then {player selectWeapon BTC_sec_weap;BTC_at_mag = currentMagazine player;player selectWeapon BTC_weap_sel;};
	if (BTC_handgun != "") then {player selectWeapon BTC_handgun;BTC_h_mag = currentMagazine player;player selectWeapon BTC_weap_sel;};
	if (BTC_prim_weap != "") then {player selectWeapon BTC_prim_weap;BTC_r_mag = currentMagazine player;player selectWeapon BTC_weap_sel;};
	hint (localize "STR_REVIVE_STARTED");
};